package com.eamois.dao.manual.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.eamois.dao.impl.BaseDaoImpl;
import com.eamois.dao.manual.EnsipEntityDao;
import com.eamois.domain.manual.EnsipEntity;
import com.eamois.domain.manual.EnsipType;
import com.eamois.pdmutil.pdmView.BomView;
import com.eamois.util.PdmEjbFactory;
import com.incors.plaf.alloy.cr;
import com.mit.plm.sql.documentManage.SqlMix;


public class EnsipEntityDaoImpl extends BaseDaoImpl<EnsipEntity> implements EnsipEntityDao{

	@Override
	public EnsipEntity findEnsipEntityByOther(final EnsipType ensipType, final String name) {
		// TODO Auto-generated method stub
		return (EnsipEntity)this.getHibernateTemplate().execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = session.createCriteria(EnsipEntity.class);
				criteria.add(Restrictions.eq("ensipType", ensipType));
				criteria.add(Restrictions.eq("name", name));
				ArrayList<EnsipEntity> list = (ArrayList<EnsipEntity>)criteria.list();
				if (list.size() == 0) {
					return null;
				}else{
					return list.get(0);
				}
			}
		});
	}

	@SuppressWarnings("unchecked")
	public Collection<EnsipEntity> findAllEnsipEntity() {
		// TODO Auto-generated method stub
		return (Collection<EnsipEntity>)this.getHibernateTemplate().execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = session.createCriteria(EnsipEntity.class);
				return criteria.list();
			}
		});
	}

	@SuppressWarnings("unchecked")
	public Collection<EnsipEntity> findEnsipEntityByOther(final EnsipType ensipType,
			final String name,final String state) {
		// TODO Auto-generated method stub
		return (Collection<EnsipEntity>)this.getHibernateTemplate().execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = session.createCriteria(EnsipEntity.class);
				if (ensipType != null) {
					criteria.add(Restrictions.eq("ensipType", ensipType));
				}
				if(!"".equals(name)){
					criteria.add(Restrictions.like("name", "%"+name+"%"));
				}
				if(!state.equals("")){
					criteria.add(Restrictions.eq("state", state));
				}
				return criteria.list();
			}
		});
	}

	@Override
	public EnsipEntity findByTiaoJian(final String name,final EnsipType ensipType,
			final String state) {
		// TODO Auto-generated method stub
		return (EnsipEntity)this.getHibernateTemplate().execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = session.createCriteria(EnsipEntity.class);
				criteria.add(Restrictions.eq("name", name));
				criteria.add(Restrictions.eq("state", state));
				criteria.add(Restrictions.eq("ensipType", ensipType));
				ArrayList<EnsipEntity> list = (ArrayList<EnsipEntity>)criteria.list();
				if (list.size() == 0) {
					return null;
				}else{
					return list.get(0);
				}
			}
		});
	}

	@Override
	public EnsipEntity findByCodeAndName(final String model, final String name) {
		// TODO Auto-generated method stub
		return (EnsipEntity)this.getHibernateTemplate().execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = session.createCriteria(EnsipEntity.class);
				criteria.add(Restrictions.eq("model", model));
				criteria.add(Restrictions.eq("name", name));
				ArrayList<EnsipEntity> list = (ArrayList<EnsipEntity>)criteria.list();
				if (list.size() == 0) {
					return null;
				}else{
					return list.get(0);
				}
			}
		});
	}

	@SuppressWarnings("unchecked")
	public Collection<EnsipEntity> findEnsipEntityByType(final EnsipType ensipType) {
		// TODO Auto-generated method stub
		return (Collection<EnsipEntity>)this.getHibernateTemplate().execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = session.createCriteria(EnsipEntity.class);
				criteria.add(Restrictions.eq("ensipType", ensipType));
				return criteria.list();
			}
		});
	}

	@SuppressWarnings("unchecked")
	public Collection<EnsipEntity> findEnsipEntityByState(final String state) {
		// TODO Auto-generated method stub
		return (Collection<EnsipEntity>)this.getHibernateTemplate().execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = session.createCriteria(EnsipEntity.class);
				criteria.add(Restrictions.eq("state", state));
				return criteria.list();
			}
		});
	}

	@Override
	public boolean updateEnsipEntity(int ensipEntityId,String bomName, String bomVar) {
		// TODO Auto-generated method stub
		SqlMix sqlMix =PdmEjbFactory.getEjbInstance();
		boolean result = false;
		String sql = "update ietm_ensip_entity set bomName ='"+bomName+"',bomVar='"+bomVar+"' where id ='"+ensipEntityId+"'";
		ArrayList<BomView> list = new ArrayList<BomView>();
		result = sqlMix.executeUpdate(sql);
	
		return result;
	}

}
